package com.yl.ydk.ui.activity

import android.content.Intent
import android.os.Build
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.bean.UserInfoBean
import com.yl.ydk.presenter.contract.BingdingPhoneActivityContract
import com.yl.ydk.presenter.impl.BingdingPhoneActivityImpl
import com.yl.ydk.utils.CommontUtils
import com.yl.ydk.utils.PreferUtil
import com.blankj.utilcode.utils.RegexUtils
import com.blankj.utilcode.utils.StringUtils
import com.blankj.utilcode.utils.ToastUtils
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_binding_phone.*

class BindingPhoneActivity : BaseActivity(), BingdingPhoneActivityContract.View,
    View.OnClickListener {


    private val bindingPhoneActivity = BingdingPhoneActivityImpl()
    private var sbPhone = StringBuffer()
    private var sbCode = StringBuffer()
    private var isInput = true

    override fun getLayoutId(): Int = R.layout.activity_binding_phone

    override fun attachView() {
        bindingPhoneActivity.attachView(this)
    }

    override fun detachView() {
        bindingPhoneActivity.detachView()
    }

    override fun initView() {

        isInput = true
        etPhone.isFocusable = true
        etPhone.isFocusableInTouchMode = true
        etPhone.requestFocus()
        etPhone.findFocus()

        etCode.isFocusable = false
        etCode.isFocusableInTouchMode = false

        btnPost.setOnClickListener(this)
        etPhoneView.setOnClickListener(this)
        etCodeView.setOnClickListener(this)
        rightCode.setOnClickListener(this)
        tvTreaty.setOnClickListener(this)

        rl0.setOnClickListener(this)
        rl1.setOnClickListener(this)
        rl2.setOnClickListener(this)
        rl3.setOnClickListener(this)
        rl4.setOnClickListener(this)
        rl5.setOnClickListener(this)
        rl6.setOnClickListener(this)
        rl7.setOnClickListener(this)
        rl8.setOnClickListener(this)
        rl9.setOnClickListener(this)
        rlDelete.setOnClickListener(this)
        val userInfoBean =
            Gson().fromJson<UserInfoBean>(PreferUtil.getInstance().userInfo, UserInfoBean::class.java)
        if (userInfoBean != null ) {
            startActivityIn(Intent(this, HomeActivity::class.java), this)
        }

    }

    override fun showError(message: String?) {
        ToastUtils.showLongToastSafe(message)
    }

    override fun getTvCode(): TextView? = tvCode

    override fun getRlCode(): RelativeLayout? = rightCode

    override fun getActivity(): BindingPhoneActivity = this

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvTreaty -> {
                startActivityIn(Intent(this, TreatyActivity::class.java), this)
            }
            R.id.btnPost -> {
                val strCode = etCode.text.toString()
                if (!checkbox.isChecked) {
                    ToastUtils.showLongToastSafe("请同意医达康平台服务协议")
                    return
                }
                if (StringUtils.isEmpty(strCode)) {
                    ToastUtils.showLongToastSafe("请输入验证码")
                    return
                }
                bindingPhoneActivity.getLogin(
                    this, "type", "phone ", "phone", etPhone.text.toString().trim(),
                    "sms_code", strCode, "device_no", Build.SERIAL,
                )
            }

            R.id.rightCode -> {
                val strPhone = etPhone.text.toString().trim()
                if (StringUtils.isEmpty(strPhone)) {
                    ToastUtils.showLongToastSafe("请输入手机号")
                    return
                }
                if (!RegexUtils.isMobileSimple(strPhone)) {
                    ToastUtils.showLongToastSafe("请输入正确手机号")
                    return
                }
                bindingPhoneActivity.getSendSmsCode(this, "phone", strPhone, "type", "login")
            }

            R.id.etPhoneView -> {
                isInput = true
                etPhone.isFocusable = true
                etPhone.isFocusableInTouchMode = true
                etPhone.requestFocus()
                etPhone.findFocus()

                etCode.isFocusable = false
                etCode.isFocusableInTouchMode = false

            }

            R.id.etCodeView -> {
                isInput = false
                etCode.isFocusable = true;
                etCode.isFocusableInTouchMode = true;
                etCode.requestFocus()
                etCode.findFocus()

                etPhone.isFocusable = false
                etPhone.isFocusableInTouchMode = false

            }

            R.id.rl0 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("0")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("0")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl1 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("1")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("1")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl2 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("2")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("2")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl3 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("3")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("3")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl4 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("4")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("4")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl5 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("5")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("5")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl6 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("6")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("6")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl7 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("7")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("7")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl8 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("8")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("8")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rl9 -> {
                if (isInput) {  // 手机号输入
                    sbPhone.append("9")
                    etPhone.setText(sbPhone.toString())
                    etPhone.setSelection(etPhone.text.length)
                } else {
                    sbCode.append("9")
                    etCode.setText(sbCode.toString())
                    etCode.setSelection(etCode.text.length)
                }
            }
            R.id.rlDelete -> {
                if (isInput) {  // 手机号删除
                    if (sbPhone.isNotEmpty()) {
                        sbPhone.deleteCharAt(sbPhone.length - 1)
                        etPhone.setText(sbPhone.toString())
                        etPhone.setSelection(etPhone.text.length)
                    }
                } else {
                    if (sbCode.isNotEmpty()) {
                        sbCode.deleteCharAt(sbCode.length - 1)
                        etCode.setText(sbPhone.toString())
                        etCode.setSelection(etPhone.text.length)
                    }
                }
            }
        }
    }

}